package com.crl.module.live
{
	import com.crl.utils.HashMap;
	
	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	/**
	 * live 服务连接共享对像;
	 * @author crl
	 * 
	 */	
	public class SharerConnection
	{
		private var connection:NetConnection;
		private var path:String;
		
		private var connectedCallBack:Function;
		
		
		private var currentCheckCount:int=0;
		private const DEFAULT_CHECKTIME:int=3;
		private var reconnectInterval:int=0;
		
		/**
		 * 引用列表; 
		 */		
		private var references:HashMap;
		
		/**
		 *  
		 * @param livePath 一个连接地址,当中包含有房间的id;
		 * 
		 */		
		public function SharerConnection(path:String)
		{
			this.path=path;
			
		}
		
		
		/**
		 * 取得引用列表; 
		 * @return 
		 * 
		 */		
		public function getReferences():HashMap{
			if(references==null){
				this.references=new HashMap();
			}
			return this.references;
		}
		
		
		public function get livePath():String{
			return this.path;
		}
		
		
		public function get isConnected():Boolean{
			return getConnection().connected;
		}
		
		/**
		 * 开始连接; 
		 * @param connCallBack
		 * 
		 */		
		public function doConnection(connCallBack:Function):void{
			if(isConnected){
				return;
			}
			
			this.connectedCallBack=connCallBack;
			if(this.connection){
				connection.removeEventListener(NetStatusEvent.NET_STATUS,status);
			}
			getConnection().connect(path);
			connection.addEventListener(NetStatusEvent.NET_STATUS,status);
		}
		
		
		/**
		 * 检查引用此连接的abstractLive数; 
		 * @return 
		 * 
		 */		
		public function checkDisconnectable():Boolean{
			if(this.references==null){
				return true;
			}
			return this.references.size()<1;
		}
		
		
		/**
		 * 当没有引用连接数,关闭连接; 
		 * 
		 */		
		public function disconnect():void{
			var b:Boolean=checkDisconnectable();
			if(b){
				getConnection().close();
			}
		}
		
		/**
		 * 取得一个连接; 
		 * @return 
		 * 
		 */		
		public function getConnection():NetConnection{
			if(connection==null){
				connection=new NetConnection();
				connection.client=new LiveClient(null);
			}
			
			return connection;
		}
		
		
		
		/**
		 * 只对成功连接做处理; 
		 * @param event
		 * 
		 */		
		public function status(event:NetStatusEvent):void{
	 		switch(event.info.code){
	 			case LiveConst.SUCCESS :
	 				var f:Function=this.connectedCallBack;
	 				if(f !=null)f();
	 				
	 				this.currentCheckCount=0;
	 				this.connectedCallBack=null;
	 				break;
	 			case LiveConst.REJECTED :
	 				//Alert.getInstance().popUp(RFMessage.generateMessage(SJMessageCode.ROOM_VIDEO_INFO_002));
	 				break;
	 			case LiveConst.FAILED :
	 				//被服务器拒绝时;
	 				if(this.currentCheckCount++>this.DEFAULT_CHECKTIME){
	 					this.currentCheckCount=0;
	 					//Alert.getInstance().popUp(RFMessage.generateMessage(SJMessageCode.ROOM_VIDEO_INFO_003));
	 				}else{
	 					//稍等待再连;(在状态抛出时直接重连,很可能出现错误);
	 					clearTimeout(this.reconnectInterval);
	 					this.reconnectInterval=setTimeout(reConnectTest,3000);
	 				}
	 				break;
	 		}
	 	}
	 	
	 	
	 	/**
	 	 * 用于连接失败的重试,没有回调的话,是不用再去做重连操作的; 
	 	 * 
	 	 */	 	
	 	private function reConnectTest():void{
	 		if(this.connectedCallBack !=null){
	 			this.doConnection(this.connectedCallBack);
	 		}	
	 	}
		
		
		/**
		 * 注销掉; 
		 * 
		 */		
		public function dispose():void{
			if(connection){
				connection.removeEventListener(NetStatusEvent.NET_STATUS,status);
				if(connection.connected)connection.close();
			}
			
			clearTimeout(this.reconnectInterval);
			
			this.references=null;
			this.connection=null;
			this.connectedCallBack=null;
			this.currentCheckCount=0;
		}
		
	}
}